home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / dvips / squeeze.c- < prev    next >
Encoding:
Text File  |  1990-02-27  |  3.0 KB  |  141 lines

  1. /*
  2.  *   This software is Copyright 1988 by Radical Eye Software.
  3.  *   All Rights Reserved.
  4.  */
  5. /*
  6.  *   This routine squeezes a PostScript file down to its
  7.  *   minimum.  We parse and then output it.
  8.  */
  9. #include "stdio.h"
  10. #define LINELENGTH (78)
  11. #define BUFLENGTH (1000)
  12. #undef putchar
  13. #define putchar(a) (void)putc(a, out) ;
  14. FILE *in, *out ;
  15. static int linepos = 0 ;
  16. static int lastspecial = 1 ;
  17. /*
  18.  *   This next routine writes out a `special' character.  In this case,
  19.  *   we simply put it out, since any special character terminates the
  20.  *   preceding token.
  21.  */
  22. specialout(c)
  23. char c ;
  24. {
  25.    if (linepos + 1 > LINELENGTH) {
  26.       putchar('\n') ;
  27.       linepos = 0 ;
  28.    }
  29.    putchar(c) ;
  30.    linepos++ ;
  31.    lastspecial = 1 ;
  32. }
  33. strout(s)
  34. char *s ;
  35. {
  36.    if (linepos + strlen(s) > LINELENGTH) {
  37.       putchar('\n') ;
  38.       linepos = 0 ;
  39.    }
  40.    linepos += strlen(s) ;
  41.    while (*s != 0)
  42.       putchar(*s++) ;
  43.    lastspecial = 1 ;
  44. }
  45. cmdout(s)
  46. char *s ;
  47. {
  48.    int l ;
  49.  
  50.    l = strlen(s) ;
  51.    if (linepos + l + 1 > LINELENGTH) {
  52.       putchar('\n') ;
  53.       linepos = 0 ;
  54.       lastspecial = 1 ;
  55.    }
  56.    if (! lastspecial) {
  57.       putchar(' ') ;
  58.       linepos++ ;
  59.    }
  60.    while (*s != 0) {
  61.       putchar(*s++) ;
  62.    }
  63.    linepos += l ;
  64.    lastspecial = 0 ;
  65. }
  66. char buf[BUFLENGTH] ;
  67. main(argc, argv)
  68. int argc ;
  69. char *argv[] ;
  70. {
  71.    int c ;
  72.    char *b ;
  73.    char seeking ;
  74.  
  75.    if (argc > 3 || (in=(argc < 2 ? stdin : fopen(argv[1], "r")))==NULL ||
  76.                     (out=(argc < 3 ? stdout : fopen(argv[2], "w")))==NULL) {
  77.       (void)fprintf(stderr, "Usage:  squeeze [infile [outfile]]\n") ;
  78.       exit(1) ;
  79.    }
  80.    (void)fprintf(out, "%%!\n") ;
  81.    while (1) {
  82.       c = getc(in) ;
  83.       if (c==EOF)
  84.          break ;
  85.       if (c=='%') {
  86.          while ((c=getc(in))!='\n') ;
  87.       }
  88.       if (c <= ' ')
  89.          continue ;
  90.       switch (c) {
  91. case '{' :
  92. case '}' :
  93. case '[' :
  94. case ']' :
  95.          specialout(c) ;
  96.          break ;
  97. case '<' :
  98. case '(' :
  99.          if (c=='(')
  100.             seeking = ')' ;
  101.          else
  102.             seeking = '>' ;
  103.          b = buf ;
  104.          *b++ = c ;
  105.          do {
  106.             c = getc(in) ;
  107.             if (b > buf + BUFLENGTH-2) {
  108.                (void)fprintf(stderr, "Overran buffer seeking %n", seeking) ;
  109.                exit(1) ;
  110.             }
  111.             *b++ = c ;
  112.             if (c=='\\')
  113.                *b++ = getc(in) ;
  114.          } while (c != seeking) ;
  115.          *b++ = 0 ;
  116.          strout(buf) ;
  117.          break ;
  118. default:
  119.          b = buf ;
  120.          while ((c>='A'&&c<='Z')||(c>='a'&&c<='z')||
  121.                 (c>='0'&&c<='9')||(c=='/')||(c=='@')||
  122.                 (c=='-')||(c=='.')||(c=='#')||(c=='|')||(c=='_')||
  123.                 (c=='=')||(c=='$')||(c=='+')||(c==':')) {
  124.             *b++ = c ;
  125.             c = getc(in) ;
  126.          }
  127.          if (b == buf) {
  128.             (void)fprintf(stderr, "Oops!  Missed a case: %c.\n", c) ;
  129.             exit(1) ;
  130.          }
  131.          *b++ = 0 ;
  132.          (void)ungetc(c, in) ;
  133.          cmdout(buf) ;
  134.       }
  135.    }
  136.    if (linepos != 0)
  137.       putchar('\n') ;
  138.    exit(0) ;
  139.    /*NOTREACHED*/
  140. }
  141.